<?php
/*
* Action 的基类，所有Action都必须继承该类
*/
namespace Lib;
class Action{
	public $serv;
	// 模板目录
	private $tpl_path;
	// 主题
	private $theme;
	// 挂件
	private $widgets;
	// 控制器
	public $act_name;
	// 页面ID
	public $page_id;
	// 请求方式
	public $request_method;
	// 模板变量
	private $data;
	public function __construct($server,$request_method){
		$this->serv = $server;
		$this->data = [];
		$this->theme = isset($this->serv->config['app']['theme']) && !empty($this->serv->config['app']['theme']) ? $this->serv->config['app']['theme'] : 'default';
		$classed_class = explode('\\',get_called_class());
		array_shift($classed_class);
		$this->tpl_path = strtolower(join('/',$classed_class));
		$this->act_name = \Lib\Common::getActionUrl();
		$this->page_id = abs(crc32($this->act_name));
		$this->assign('page_id',$this->page_id);
		$this->request_method = $request_method;
		if(method_exists($this, '_init')){
			call_user_func(array($this,'_init'));
		}
	}	
	public function doGet(){
		return $this->serv->http404();
	}
	public function doPost(){
		return $this->serv->http404();
	}
	public function doUpdate(){
		return $this->serv->http404();
	}
	public function doDelete(){
		return $this->serv->http404();
	}
	public function doAdd(){
		return $this->serv->http404();
	}

	protected function assign(){
		$argvs = func_get_args();
		if(empty($argvs)){
			return;
		}
		if(is_array($argvs[0])){
			return $this->data = array_merge($this->data,$argvs[0]);
		}elseif(count($argvs)>1){
			return $this->data[$argvs[0]] = $argvs[1];
		}
	}

	protected function display($tpl_file,$data = array()){
		$data = array_merge($this->data,$data);
		extract($data);
		include($this->tpl($tpl_file,$this->theme));
		unset($data);
		$this->data = [];
	}

	public function tpl($tpl_file,$theme='default'){
		if(strpos($tpl_file, ':')){
			list($dir,$tpl_file) = explode(':',$tpl_file,2);
		}else{
			$dir = $this->tpl_path;
		}
		$shm_file = SHM_PATH.'tpls/'.$theme.'/'.$dir.'/'.$tpl_file;
		$default_shm_file = SHM_PATH.'tpls/default/'.$dir.'/'.$tpl_file;
		$real_tpl_file = TPL_PATH.$theme.'/'.$dir.'/'.$tpl_file;
		$default_real_tpl_file = TPL_PATH.'default/'.$dir.'/'.$tpl_file;
		if(is_file($shm_file)){
			if(!defined('DEBUG')){return $shm_file;}
		}elseif($theme!='default' && is_file($default_shm_file)){
			return $default_shm_file;
		}

		if($theme != 'default' && !is_file($tpl_file)){
			$real_tpl_file = $default_real_tpl_file;
			$shm_file = $default_shm_file;
		}
		
		if(!is_file($shm_file) || filemtime($real_tpl_file)>filemtime($shm_file)){
			if(!is_file($real_tpl_file)){
				if(is_file($shm_file))
					unlink($shm_file);
				throw new \Exception("Template file <font color='red'>".str_replace(APP_PATH,'',$real_tpl_file)."</font> not exist!", 300);
			}
			$shm_dir = dirname($shm_file);
			if(!is_dir($shm_dir)){
				mkdir($shm_dir,0755,true);
			}
			copy($real_tpl_file, $shm_file);
		}
		return $shm_file;
	}

	public function widget($name,$data=[]){
		if(!isset($this->widgets[$name])){
			$w_name = ucwords($name);
			$widget = '\\Widget\\'.$w_name.'\\'.$w_name;
			if(!class_exists($widget)){
				throw new \Exception("Widget <font color='red'>{$name}</font> not exist",400);
			}
			$this->widgets[$name] = new $widget($this->page_id);
		}
		$this->widgets[$name]->init()->show($data);
	}

	public function checkParam($param,$to_url=null){
		if($to_url == null ){
			if(array_key_exists('HTTP_REFERER',$_SERVER)){
				$referer = $_SERVER['HTTP_REFERER'];
			}
			if(!empty($referer)){
				$start = strpos($referer,site_url());
				$to_url = substr($referer,$start+strlen(site_url()));
			}else{
				$to_url = '/os/index';
			}
		}
		
		if (empty ( $param )) {
			throw new \Exception("缺少必要的参数", 0);
		}
	}

	public function jsonError($msg){
		return $this->json(array('error'=>1,'msg'=>$msg));
	}

	public function jsonSuccess($msg){
		return $this->json(array('error'=>0,'msg'=>$msg));
	}

	public function json($data){
		return json_encode($data,JSON_UNESCAPED_UNICODE);
	}

	public function validate($data,$need){
		if(empty($data) || empty($need)){
			return false;
		}
		$need_keys = array_keys($need);
		foreach($need_keys as $need_key){
			if(!isset($data[$need_key]) || empty($data[$need_key])){
				return $need[$need_key];
			}
		}
		return true;
	}

}